package com.qc.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qc.common.DateUtil;
import com.qc.common.Session;
import com.qc.common.Util;
import com.qc.entity.BusinessCategoryEntity;
import com.qc.entity.BusinessFollow;
import com.qc.entity.BusinessReceive;
import com.qc.entity.Coupon;
import com.qc.entity.FootprintEntity;
import com.qc.entity.MerchantEntity;
import com.qc.entity.UserSecScheduleEntity;
import com.qc.entity.search.MerchantSearchEntity;
import com.qc.service.BusinessCategoryService;
import com.qc.service.BusinessFollowService;
import com.qc.service.BusinessReceiveService;
import com.qc.service.CouponService;
import com.qc.service.FootprintService;
import com.qc.service.MerchantService;
import com.qc.service.UserSecScheduleService;

import net.sf.json.JSONObject;

@Controller
public class MobileMerchantController extends BaseController{

	@Resource(name="merchantService")
	private MerchantService merchantService;
	
	@Resource(name="businessReceiveService")
	private BusinessReceiveService businessReceiveService;
	
	@Resource(name="businessFollowService")
	private BusinessFollowService businessFollowService;
	
	@Resource(name="couponService")
	private CouponService couponService;
	
	@Resource(name="userSecScheduleService")
	private UserSecScheduleService userSecScheduleService;
	
	@Resource(name="footprintService")
	private FootprintService footprintService;

	@Resource(name="businessCategoryService")
	private BusinessCategoryService businessCategoryService;
	
	/**
	 * 获取商家所有发布信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetAllMerchant", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetAllMerchant(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		MerchantSearchEntity merchantSearchEntity = (MerchantSearchEntity)JSONObject.toBean(JSONObject.fromObject(p1), MerchantSearchEntity.class);
		
		List<MerchantEntity> list = merchantService.getAllMerchant(merchantSearchEntity,merchantSearchEntity.getPageNo(), merchantSearchEntity.getPageSize());
		
		return returnSuccessResult(list);
	}
	
	/**
	 * 商家详情页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetMerchantInfo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetMerchantInfo(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		
		long userId = Session.getUserIdfromMobileSession(p2);
		
		MerchantEntity merchantEntity2 = (MerchantEntity)JSONObject.toBean(JSONObject.fromObject(p1), MerchantEntity.class);
		
		MerchantEntity merchantEntity =  merchantService.getMerchantId(merchantEntity2.getShopId());

		//查询该用户 是否关注此商家
		int follow = businessFollowService.getUserFollowId(userId, merchantEntity2.getShopId());
		if (follow >0) {
			merchantEntity.setUserFollow(1);
		}else {
			merchantEntity.setUserFollow(0);
		}
		
		int num = userSecScheduleService.getMerchantUserCount("", merchantEntity2.getShopId());
		//当前商家 所有用户代言
		List<UserSecScheduleEntity> scheduleEntities = userSecScheduleService.getAllMerchantUser("",merchantEntity2.getShopId(),1, 10);
		
		//当前商家 所有 优惠券 该用户是否领取
 		List<Coupon> listCoupon = couponService.listAllShopId(userId,merchantEntity2.getShopId());
		
		merchantEntity.setListCoupon(listCoupon);
		merchantEntity.setSecScheduleNum(num);
		merchantEntity.setSecScheduleEntity(scheduleEntities);
				
		return returnSuccessResult(merchantEntity);
	}
	
	/**
	 * 所有用户发布 包含 当前商家列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetMerchantUserShop", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetMerchantUserShop(HttpServletRequest request, HttpServletResponse response) {
		String p1 = request.getParameter("p1");
		
		MerchantEntity merchantEntity2 = (MerchantEntity)JSONObject.toBean(JSONObject.fromObject(p1), MerchantEntity.class);
		
		List<UserSecScheduleEntity> list = userSecScheduleService.getAllMerchantUser("",merchantEntity2.getShopId(),merchantEntity2.getPageNo(), merchantEntity2.getPageSize());
		
		return returnSuccessResult(list);
	}
	
	/**
	 * 用户 关注商家
	 */
	@RequestMapping(value = "/MobileGetMerchanFollow", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetMerchanFollow(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		BusinessFollow businessFollow = (BusinessFollow)JSONObject.toBean(JSONObject.fromObject(p1), BusinessFollow.class);
		
		businessFollow.setUserId(userId);
		
		businessFollowService.saveBusinessFollow(businessFollow);
		
		merchantService.updateFollowNumber(businessFollow.getShopId());
		
		return returnSuccessResult("关注成功");
	}
	
	/**
	 * 用户 取消关注商家
	 */
	@RequestMapping(value = "/MobileGetMerchanFollowCancel", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetMerchanFollowCancel(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		BusinessFollow businessFollow = (BusinessFollow)JSONObject.toBean(JSONObject.fromObject(p1), BusinessFollow.class);
		
		businessFollowService.deleteBusinessFollow(businessFollow.getShopId(),userId);
		 
		MerchantEntity merchantEntity = merchantService.getMerchantId(businessFollow.getShopId());
		
		if (merchantEntity.getFollowNumber() != 0) {
			merchantService.updateFollowNumberCancel(businessFollow.getShopId());
		}
		
		return returnSuccessResult("取消成功");
	}
	
	/**
	 * 用户 领取优惠券
	 */
	@RequestMapping(value = "/MobileGetMerchanReceive", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetMerchanReceive(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		BusinessReceive businessReceive = (BusinessReceive)JSONObject.toBean(JSONObject.fromObject(p1), BusinessReceive.class);
		
		Coupon coupon = couponService.getByCouponId(businessReceive.getCouponId());
		
		int num = businessReceiveService.getSerchReceive(businessReceive.getShopId(), userId, businessReceive.getCouponId());
		if (num > 0) {
			return returnSuccessResult("已领取");
		}else {
			
			if (coupon.getNumNumber() == 0) {
				return returnSuccessResult("已领完");
			}else {
				businessReceive.setUserId(userId);
				businessReceive.setCouponNumber(DateUtil.getNowTimeUnFormat() + Util.get6code());
				
				businessReceiveService.saveBusinessReceive(businessReceive);
				
				coupon.setNumNumber(coupon.getNumNumber() - 1);
				
				coupon.setbId(coupon.getbId());
				
				couponService.updateCouponId(coupon);
				
				return returnSuccessResult("领取成功");
			}
		}
		
	}
	
	/**
	 * 商家 分类
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetMerchantCategor", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetMerchantCategor(HttpServletRequest request, HttpServletResponse response) {
		String p1 = request.getParameter("p1");
		
		MerchantEntity merchantEntity2 = (MerchantEntity)JSONObject.toBean(JSONObject.fromObject(p1), MerchantEntity.class);
		
		List<UserSecScheduleEntity> list = userSecScheduleService.getAllMerchantUser("",merchantEntity2.getShopId(),merchantEntity2.getPageNo(), merchantEntity2.getPageSize());
		
		return returnSuccessResult(list);
	}
	
	/**
	 * 商家 浏览数 添加
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetMerchanBrowseNumber", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetMerchanBrowseNumber(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		
		MerchantEntity merchantEntity = (MerchantEntity)JSONObject.toBean(JSONObject.fromObject(p1), MerchantEntity.class);
		
		merchantService.updateBrowseNumber(merchantEntity.getShopId());
		
		return returnSuccessResult("浏览成功");
	}
	
	
	/**
	 * 用户发布 浏览足迹 添加
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetSaveFootprint", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetSaveFootprint(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		
		FootprintEntity footprintEntity = (FootprintEntity)JSONObject.toBean(JSONObject.fromObject(p1), FootprintEntity.class);
		
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0) {
			return returnFailedResult("");
		}else if (userId == footprintEntity.getReceiveId()){
			return returnFailedResult("相同用户，自己查看自己");
		}
		
		FootprintEntity footprintEntity2 = footprintService.getFootprintById(footprintEntity.getSsId(), userId);
		if (footprintEntity2.getSsId() != 0) {
			
			footprintService.updateFootprint(footprintEntity2.getbId());
			
		}else{
			footprintEntity.setUserId(userId);
			footprintService.saveFootprint(footprintEntity);
		}
		
		return returnSuccessResult("足迹添加成功");
	}
	
	/**
	 * 当前用户发布 浏览的足迹
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetFootprintList", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetFootprintList(HttpServletRequest request, HttpServletResponse response) {
		
		String p1 = request.getParameter("p1");
		
		FootprintEntity footprintEntity = (FootprintEntity)JSONObject.toBean(JSONObject.fromObject(p1), FootprintEntity.class);
		
		List<FootprintEntity> list = footprintService.listAlFootprint(footprintEntity.getSsId(), 0, footprintEntity.getPageNo(), footprintEntity.getPageSize());
		for (FootprintEntity entity : list) {
			
			entity.setModifiedTime(entity.getModifiedTime().substring(0,19));
		}

		return returnSuccessResult(list);
	}
	
	/**
	 * 商家分类
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetBusinessCategoryList", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetBusinessCategoryList(HttpServletRequest request, HttpServletResponse response) {
		
		List<BusinessCategoryEntity> listCategoryOne = businessCategoryService.getAllLevleOne();
		for (BusinessCategoryEntity entity : listCategoryOne) {
			
			List<BusinessCategoryEntity> listCategoryTwo = businessCategoryService.getAllLevleTwo(String.valueOf(entity.getbId()));
			
			entity.setListCategory(listCategoryTwo);
			
		}
		
		return returnSuccessResult(listCategoryOne);
	}
	
	public static void main(String[] args) {
		System.out.println(Math.round(Math.random()*(5-1)+1));
	}
	
	
	
}
